# -*- coding: utf-8 -*-
# File : mysql_util.py
# Author: taoyahui
# Date : 2021/3/23

import pymysql


def get_conn(host='127.0.0.1', port=13308, user='root', passwd='qwerasdf@123456', db='patch_distribution'):
    conn = pymysql.connect(host=host, port=port, user=user, passwd=passwd, charset='utf8', db=db)
    return conn


def get_cursor(conn):
    return conn.cursor()


def exec_sql(conn, cursor, sql):
    try:
        cursor.execute(sql)
        result = cursor.fetchall()
        conn.commit()
        return result
    except BaseException as e:
        conn.rollback()
        print('Error: %s' % e)


def release(conn, cursor):
    if cursor:
        cursor.close()
    if conn:
        conn.close()


if __name__ == '__main__':
    conn = get_conn()
    cursor = get_cursor(conn)
    cve_id = 'CVE-2019-3846'
    sql = f"select count(1) from cve where cve_id = '{cve_id}'"
    print(exec_sql(conn, cursor, sql)[0][0])
